package http_call

import (
	"bytes"
	"aifuture.cloud/pkg/logging"
	"io/ioutil"
	"net/http"
	"unsafe"
)

func Post(uri string, params []byte) (string, error) {
	logging.Info(uri, "request param : ", string(params))
	res, err := http.Post(uri, "application/json;charset=utf-8", bytes.NewBuffer(params))
	if err != nil {
		logging.Info(uri, "err: ", err.Error())
		return "", err
	}

	defer res.Body.Close()

	content, err := ioutil.ReadAll(res.Body)
	if err != nil {
		logging.Info(uri, "err: ", err.Error())
		return "", err
	}
	str := (*string)(unsafe.Pointer(&content)) //转化为string,优化内存
	logging.Info(uri, "res :", *str)
	return *str, nil
}

func Get(uri string) (string, error) {
	logging.Info(uri)
	res, err := http.Get(uri)
	if err != nil {
		logging.Info(uri, "err: ", err.Error())
		return "", err
	}

	defer res.Body.Close()

	content, err := ioutil.ReadAll(res.Body)
	if err != nil {
		logging.Info(uri, "err: ", err.Error())
		return "", err
	}
	str := (*string)(unsafe.Pointer(&content)) //转化为string,优化内存
	logging.Info(uri, "res :", *str)
	return *str, nil
}
